<template>
  <div id="viewQuestionView">
    <a-row :gutter="[24, 24]">
      <a-col :md="12" :xs="24">
        <a-tabs default-active-key="2">
          <a-tab-pane key="question" title="question" default>
            <a-card v-if="question" :title="question.title">
              <a-descriptions
                title="JudgeConfig"
                :column="{ xs: 1, md: 3, lg: 4 }"
              >
                <a-descriptions-item label="timeLimit">
                  {{ question.judgeConfig.timeLimit ?? 0 }}</a-descriptions-item
                >

                <a-descriptions-item label="timeLimit">
                  {{
                    question.judgeConfig.memoryLimit ?? 0
                  }}</a-descriptions-item
                >

                <a-descriptions-item label="timeLimit">
                  {{
                    question.judgeConfig.stackLimit ?? 0
                  }}</a-descriptions-item
                >
              </a-descriptions>

              <MdView :value="question.content || ' '" />
              <template #extra>
                <a-space wrap>
                  <a-tag
                    v-for="(tag, index) of question.tags"
                    :key="index"
                    color="grey"
                    >{{ tag }}</a-tag
                  >
                </a-space>
              </template>
            </a-card>
          </a-tab-pane>
          <a-tab-pane key="comment" title="comment" disabled>
            comment
          </a-tab-pane>
          <a-tab-pane key="answer" title="answer">
            no auth to view answer
          </a-tab-pane>
        </a-tabs>
      </a-col>

      <a-col :md="12" :xs="24">
        <a-form :model="form" layout="inline">
          <a-form-item field="language" label="language">
            <a-select
              :style="{ width: '320px' }"
              placeholder="Please select language"
              v-model="form.language"
            >
              <a-option>java</a-option>
              <a-option>cpp</a-option>
              <a-option>go</a-option>
            </a-select>
          </a-form-item>
        </a-form>

        <CodeEditor
          :value="form.code"
          :language="form.language"
          :handle-change="changeCode"
        />
        <a-button type="primary" style="margin: 20px" @click="doSubmit"
          >Submit</a-button
        >
      </a-col>
    </a-row>
  </div>
</template>

<script setup lang="ts">
import { ref, withDefaults, defineProps, onMounted } from "vue";
import {
  QuestionControllerService,
  QuestionSubmitAddRequest,
  QuestionSubmitControllerService,
  QuestionVO,
} from "../../../generated";
import message from "@arco-design/web-vue/es/message";
import CodeEditor from "@/components/CodeEditor.vue";
import MdView from "@/components/MdView.vue";

const question = ref<QuestionVO>();

const form = ref<QuestionSubmitAddRequest>({
  language: "java",
  code: "",
});

interface Props {
  id: string;
}

const props = withDefaults(defineProps<Props>(), {
  id: () => "",
});

const doSubmit = async () => {
  if (!question.value?.id) {
    return;
  }
  const res = await QuestionSubmitControllerService.doQuestionSubmitUsingPost({
    ...form.value,
    questionId: question.value?.id,
  });
  if (res.code === 0) {
    message.success("submit succeed!" + res.message);
  } else {
    message.error("submit failed!" + res.message);
  }
};

const loadData = async () => {
  const res = await QuestionControllerService.getQuestionVoByIdUsingGet(
    props.id as any
  );
  if (res.code === 0) {
    question.value = res.data as any;
  } else {
    message.error("require question failed!", res.message as any);
  }
};

const changeCode = (val: string | undefined) => {
  form.value.code = val;
};

onMounted(() => {
  loadData();
});
</script>

<style>
#viewQuestionView .arco-space-horizontal .arco-space-item {
  margin-bottom: 0 !important;
}
</style>
